package com.canlihao.ui.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.canlihao.ui.R;
import com.canlihao.ui.base.BaseApplication;
import com.canlihao.ui.base.BaseWhiteTopbarActivity;
import com.canlihao.ui.bean.FunctionBean;
import com.canlihao.ui.mvp.activity.LoginActivityMvp;
import com.canlihao.ui.mvp.dialog.DialogLoading;
import com.canlihao.ui.mvp.dialog.Dialogwx;
import com.canlihao.ui.utils.IntentUtil;
import com.canlihao.ui.utils.MyLog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseWhiteTopbarActivity {
    //图标
    int[] ims = {R.mipmap.shuffling_banner,R.mipmap.jindu,R.mipmap.login,
            R.mipmap.item,R.mipmap.dialog_fragment,R.mipmap.signature,
            R.mipmap.permission,R.mipmap.mvp,R.mipmap.wxshare};
    String[] title = {"banner","loading","login","item","dialogFragment","signature","permission","mvp","share"};
    @BindView(R.id.rec_home_item)
    RecyclerView recHomeItem;
    ItemgraAdpter itemgraAdpter;
    ArrayList<FunctionBean> functionBeans = new ArrayList<>();
    float xx = 0f;
    @Override
    protected void initView() {
        super.initView();
        mTopBar.setTitle("first one");
        mTopBar.removeAllLeftViews();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        super.initData();
        Display defaultDisplay = getWindowManager().getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        int x = point.x;
        xx = x/3;
        int y = point.y;
        itemgraAdpter = new ItemgraAdpter(R.layout.item_home_one, getFunctionBeans());
        recHomeItem.setLayoutManager(new GridLayoutManager(this, 3));
        recHomeItem.setAdapter(itemgraAdpter);

    }
    public ArrayList<FunctionBean> getFunctionBeans() {
        for (int i = 0; i < ims.length; i++) {
            FunctionBean functionBean = new FunctionBean();
            functionBean.title = title[i];
            functionBean.img = ims[i];
            functionBeans.add(functionBean);
        }
        return functionBeans;
    }
    class ItemgraAdpter extends BaseQuickAdapter{
        public ItemgraAdpter(int layoutResId, List data) {
            super(layoutResId, data);
        }
        @Override
        protected void convert(BaseViewHolder helper, Object item) {
            FunctionBean func = (FunctionBean) item;

                    ViewGroup.LayoutParams pp = helper.getView(R.id.wenzhengLayout).getLayoutParams();
                    pp.width = (int) xx;
                    pp.height = (int) xx;
            helper.getView(R.id.wenzhengLayout).setLayoutParams(pp);
            helper.getView(R.id.iv_item_home_head).setBackgroundResource(func.img);
            ((TextView) helper.getView(R.id.tv_type)).setText(func.title);
            helper.getView(R.id.wenzhengLayout).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (func.title){
                        case "banner":
                            startActivity(new Intent(MainActivity.this,BannerActivity.class));
                            break;
                        case "loading":
                            startActivity(new Intent(MainActivity.this,LoadingActivity.class));
                            break;
                        case "permission":
                            startActivity(new Intent(MainActivity.this,PermissionActivity.class));
                            break;
                        case "login":
                            startActivity(new Intent(MainActivity.this,LoginActivity.class));
                            break;
                        case "mvp":
                            IntentUtil.get().goActivity(MainActivity.this, LoginActivityMvp.class);
                            break;
                        case "share":
                            Dialogwx dialogLoading = new Dialogwx(MainActivity.this,R.style.custom_dialog);
                            dialogLoading.show();
//                            sum();
//                            WXTextObject textObj = new WXTextObject();
//                            textObj.text = "第一个分享";
//
//                            //用 WXTextObject 对象初始化一个 WXMediaMessage 对象
//                            WXMediaMessage msg = new WXMediaMessage();
//                            msg.mediaObject = textObj;
//                            msg.description = "第一个分享";
//
//                            SendMessageToWX.Req req = new SendMessageToWX.Req();
//                            req.transaction = buildTransaction("text");
//                            req.message = msg;
//                            req.scene = SendMessageToWX.Req.WXSceneSession;
//                            //调用api接口，发送数据到微信
//                            BaseApplication.getWxApi().sendReq(req);

                            //初始化一个WXWebpageObject，填写url

                            break;
                        default:
                            break;
                    }

                }
            });
        }
    }
    Double muS = 0d;
    List<Double> allsn = new ArrayList<>();
    List<Double> endsu = new ArrayList<>();
    private void sum(){
        if(allsn != null && endsu != null){
            allsn.clear();
            endsu.clear();
        }
        allsn.add(100d);
        allsn.add(23d);
        allsn.add(34d);
        allsn.add(66d);
        allsn.add(43d);
        allsn.add(222d);
        allsn.add(15d);
        allsn.add(77d);
        allsn.add(99d);
        allsn.add(908d);
        for(int i = 0;i<allsn.size();i++){
            muS = muS+allsn.get(i);
        }
        MyLog.i("李魏","bsum="+muS);
        for(int i = 0;i<allsn.size();i++){
            endsu.add(allsn.get(i)/muS);
                MyLog.i("李魏","endsu.add(allsn.get(i)/muS)="+allsn.get(i)/muS);
        }


    }

}
